/**
 *    Copyright (C) 2008, 2009  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#ifndef _CUBEFS_CORE_BTREE_PAGES_PARTS_HPP
#define	_CUBEFS_CORE_BTREE_PAGES_PARTS_HPP


#include <cubefs/core/tools/reflection.hpp>
#include <cubefs/core/container/container.hpp>
#include <cubefs/core/btree/pages/node_base.hpp>
#include <cubefs/core/btree/names.hpp>


namespace cubefs    {
namespace ctr       {

using namespace cubefs::btree;

#pragma pack(1)

template <typename PageMgr>
class BTreeMetadata
{
    BigInt model_name_;

public:
    BTreeMetadata() {}

    BigInt &model_name() {
        return model_name_;
    }

    const BigInt &model_name() const {
        return model_name_;
    }

    MetadataList GetFields(Long &abi_ptr) const {
        MetadataList list;
        FieldFactory<BigInt>::create(list, model_name_, "MODEL_NAME", abi_ptr);
        return list;
    }
};

CUBEFS_TYPE_FACTORY_BEGIN1(RootProfile, BTreeRootMetadataFactory, TheModelName)
        
    typedef typename ContainerTypesLocator<Profile>::PageManagerType         	PageMgr;

public:
    typedef BTreeMetadata<PageMgr>                                              Type;
    
CUBEFS_TYPE_FACTORY_END





CUBEFS_PAGE_PART_BEGIN1(RootNodeMetadataName, TheModelName)

    typedef typename TypeLocator<typename Base::Profile, BTreeRootMetadataFactory<TheModelName> >::Type       Metadata;

private:
    Metadata metadata_;
public:

    PagePart(): Base(), metadata_() {
    	cout<<"RootPage.id="<<Base::id().value()<<endl;
    	init();
    }

    void init() {
    	Base::init();
    	Base::set_root(true);
    }

    const Metadata &metadata() const
    {
        return metadata_;
    }

    Metadata &metadata()
    {
        return metadata_;
    }

    void BuildFieldsList(MetadataList &list, Long &abi_ptr) const
    {
        Base::BuildFieldsList(list, abi_ptr);
        FieldFactory<Metadata>::create(list,  metadata(), "ROOT_METADATA", abi_ptr);
    }

    //This part must not contain CopyFrom method because it is
    //contained only in root pages

CUBEFS_PAGE_PART_END


#pragma pack()

}
}


#endif // _CUBEFS_CORE_BTREE_PAGES_PARTS_HPP
